<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Vue源码开发-生命周期合并07 | 北城韩雨</title><meta name="keywords" content="Vue"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue源码开发-生命周期合并07">
<meta property="og:url" content="http://example.com/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Vue">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475">
<meta property="article:published_time" content="2021-01-05T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-05T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue源码开发-生命周期合并07',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-01-06 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue源码开发-生命周期合并07</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-01-05T16:00:00.000Z" title="更新于 2021-01-06 00:00:00">2021-01-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%BA%90%E7%A0%81/">源码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue源码开发-生命周期合并07"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Vue源码开发之生命周期合并"><a href="#Vue源码开发之生命周期合并" class="headerlink" title="Vue源码开发之生命周期合并"></a>Vue源码开发之生命周期合并</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先我要明确一点  函数有三个功能 第一个 当作普通函数执行 第二个<span class="keyword">new</span> 执行创造实例 第三个 当作对象去使用 所以对象是会有一些方法 比如 Vue.mixins Vue.Componet Vue.directive 等等</span><br><span class="line"><span class="number">2.</span> 这里我们的生命周期实现 就是用 Vue.mixins 去实现的 当然不止生命周期 比如我们实例上的mixins 也可以把不同文件的data methods 混入进去</span><br></pre></td></tr></table></figure>
<h2 id="混合的概念"><a href="#混合的概念" class="headerlink" title="混合的概念"></a>混合的概念</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先会在 Vue对象上 有一个options 属性 这里是总的池子 也就是你混入其他的函数 或者属性的时候需要向这个options属性里去合并</span><br><span class="line"><span class="number">2.</span> 每合并一次我们的属性都是按顺序排列的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一次这个上面 </span></span><br><span class="line">Vue.options = &#123;&#125; </span><br><span class="line"><span class="comment">// 第一次调用</span></span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">	created:<span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">111</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 调用之后 向options里合并了你的函数 建就是你的created</span></span><br><span class="line">Vue.options = &#123;<span class="attr">created</span>:[<span class="built_in">console</span>.log(<span class="number">111</span>)]&#125;</span><br><span class="line"><span class="comment">// 第二次调用</span></span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">	created:<span class="function"><span class="keyword">function</span> <span class="title">b</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">222</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 调用之后 接着还是向Vue.options 里合并 在合并的过程中发现 有created的键名了 就向这个键名的数组里合并函数 合并之后</span></span><br><span class="line">Vue.options = &#123;<span class="attr">created</span>:[a,b]&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 直到最后都已经合并完毕了 </span><br><span class="line"><span class="number">2.</span> 我们就开始循环调用</span><br><span class="line"><span class="number">3.</span> 当然这里不单单是我们的created。包括我们实例上的data 和 methods 计算属性 watch等等 都可以</span><br></pre></td></tr></table></figure>
<h2 id="initGlobalApi函数"><a href="#initGlobalApi函数" class="headerlink" title="initGlobalApi函数"></a>initGlobalApi函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这个函数就我们所说的 把Vue 当作对象去使用 它上面 会定义写静态的方法等等</span><br><span class="line"><span class="number">2.</span> 比如我们下面要写的混入 mixins 方法</span><br><span class="line"><span class="number">3.</span> 当我们初始化了数据 更新函数_update _render 等等 然后就开始初始化我们的静态方法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; initGlobalApi &#125; <span class="keyword">from</span> <span class="string">&#x27;./global-api/index.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;initMixin&#125; <span class="keyword">from</span> <span class="string">&#x27;./init.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; lifecycleMixin &#125; <span class="keyword">from</span> <span class="string">&#x27;./lifecycle.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; renderMixin &#125; <span class="keyword">from</span> <span class="string">&#x27;./vdom/index.js&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这里我为了放便文件 分割 所以我们使用了构造函数的写法 而没有采用ES6 的class 类</span></span><br><span class="line"><span class="comment">// 如果采用 class 写法 我们在原型上写方法 只能写在class 里面 </span></span><br><span class="line"><span class="comment">// class Vue &#123;</span></span><br><span class="line"><span class="comment">//     a()&#123;&#125;</span></span><br><span class="line"><span class="comment">//     b()&#123;&#125;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果采用 构造函数写法 我们就可以 在原型prototype上 把方法都拆分到不同的文件中去</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Vue</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>._init(options)</span><br><span class="line">&#125;</span><br><span class="line">initMixin(Vue)</span><br><span class="line">lifecycleMixin(Vue) <span class="comment">//扩展我们原型上的_update方法 主要为了把虚拟DOM渲染到真实DOM</span></span><br><span class="line">renderMixin(Vue)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//20201 0219 静态方法 比如 Vue.component Vue.mixins  Vue.directive 等等</span></span><br><span class="line">initGlobalApi(Vue)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Vue</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 2021 0219 生命周期的混合 global-api / index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">initGlobalApi</span>(<span class="params">Vue</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 1. 我们的Component 和 mixin 还有 directive等属性 其实都是把Vue当做对象</span></span><br><span class="line">    <span class="comment">// 然后这些都是他对象上的一些方法</span></span><br><span class="line">    <span class="comment">// 这些方法都在 options属性上</span></span><br><span class="line">    Vue.options = &#123;&#125; <span class="comment">// Vue.components Vue.directive等等 都在这个options里</span></span><br><span class="line">    Vue.mixin = <span class="function"><span class="keyword">function</span>(<span class="params">mixin</span>)</span>&#123;</span><br><span class="line">      <span class="comment">//这里的this 谁调用的就是谁 首先这里我们方便举例 会用Vue.mixin去调用 所以这里的this 指的就是 Vue这个对象 那么他上面就是一些options的数据 接下来 就开始做合并的操作</span></span><br><span class="line">      <span class="comment">// 这里需要考虑的问题是 当我们用户把Vue当作构造函数去使用的时候 实例上也会传递进来一些函数 所以这里我们也要合并一下 </span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.options,<span class="string">&#x27;---this&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="mergeOptions"><a href="#mergeOptions" class="headerlink" title="mergeOptions"></a>mergeOptions</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这个就是我们合并的方法</span><br><span class="line"><span class="number">2.</span> 思路就是当调用一次mixin 传入进来的是个对象 第二次也是 所以我们核心逻辑 就是向 Vue.options 合并对象 把相同的键名 只保留一个 相对键名对应的方法都放在一个数组里</span><br><span class="line"><span class="number">3.</span> 这里我们首先只考虑生命周期   因为除了生命周期以外 我们还有 data watcher methods 等等</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// global-api / index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; mergeOptions &#125; <span class="keyword">from</span> <span class="string">&quot;../util&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">initGlobalApi</span>(<span class="params">Vue</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 1. 我们的Component 和 mixin 还有 directive等属性 其实都是把Vue当做对象</span></span><br><span class="line">    <span class="comment">// 然后这些都是他对象上的一些方法</span></span><br><span class="line">    <span class="comment">// 这些方法都在 options属性上</span></span><br><span class="line">    Vue.options = &#123;&#125; <span class="comment">// Vue.components Vue.directive等等 都在这个options里</span></span><br><span class="line">    Vue.mixin = <span class="function"><span class="keyword">function</span>(<span class="params">mixin</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//2.开始合并的函数 </span></span><br><span class="line">        <span class="comment">//合并的的操作有两次</span></span><br><span class="line">        <span class="comment">//第一次 当我们把Vue当作对象去使用的时候 我们会调用Vue.mixin方法 然后把他传进来的 去合并到我们的options上</span></span><br><span class="line">        <span class="comment">// Vue.mixin(&#123;created:function a ()&#123;...&#125;&#125;) 需要合并到我们的Vue.options上</span></span><br><span class="line">        <span class="comment">// 第二次合并 就是当我们Vue 当作构造函数 去实例化对象的时候 还要把我们实例化的选项合并到我们的上一次合并好的地方</span></span><br><span class="line">        <span class="built_in">this</span>.options = mergeOptions(<span class="built_in">this</span>.options,mixin)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// utils.js</span></span><br><span class="line"></span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">    created:<span class="function"><span class="keyword">function</span> <span class="title">a</span> </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;我是第一次调用mixin&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">    created: <span class="function"><span class="keyword">function</span> <span class="title">b</span></span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;我是第二次调用mixin&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2021 02 19 生命周期合并 我们调用一次 mixin的时候都会走到这个函数里 然后把 Vue.options 和 调用 Vue.mixin的参数传入进去</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">mergeOptions</span>(<span class="params">parent,child</span>)</span>&#123;</span><br><span class="line">  <span class="comment">// 1. 所以我们第一次 parent 就是 Vue.options 是个空对象</span></span><br><span class="line">    <span class="keyword">const</span> options = &#123;&#125;</span><br><span class="line">    <span class="comment">//6. 第二次我们调用Vue.mixin的时候 因为上一次调用已经让我们的Vue.options变成 Vue.options&#123;created:[a]&#125; 这样了 所以 就会走到 下面的循环里 然后在接着去合并</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> parent)&#123; </span><br><span class="line">      <span class="comment">// 2. 所以第一次不会走这里</span></span><br><span class="line">        mergeField(key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> child)&#123; </span><br><span class="line">      <span class="comment">//3.child就是你调用Vue.mixin 传入的参数 可能是&#123;created:function a ()&#123;&#125;&#125;等等</span></span><br><span class="line">        <span class="keyword">if</span>(!parent.hasOwnProperty(key))&#123;</span><br><span class="line">        <span class="comment">// 4. 由于我们第一次 Vue.options里没有你传入的 created这个字段 所以我们要开始合并</span></span><br><span class="line">        <span class="comment">// 合并的时候 就是调取这个函数  </span></span><br><span class="line">            mergeField(key)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">mergeField</span>(<span class="params">key</span>)</span>&#123;</span><br><span class="line">       <span class="comment">// 5. 这里现在就是做的合并操作 我们先省略</span></span><br><span class="line">       <span class="comment">// 结果就是 把我们第一次调用Vue.mixin传入的&#123;created:function a ()&#123;&#125;&#125; 合并到了 Vue.options里</span></span><br><span class="line">      <span class="comment">// 合并后的结果是 Vue.options&#123;created:[a]&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> options</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="策略模式"><a href="#策略模式" class="headerlink" title="策略模式"></a>策略模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 我们知道 我们合并的东西有很多 生命周的就有 <span class="string">&#x27;beforeCreate&#x27;</span>,<span class="string">&#x27;created&#x27;</span>,<span class="string">&#x27;beforeMount&#x27;</span>,<span class="string">&#x27;mounted&#x27;</span>,<span class="string">&#x27;beforeUpdate&#x27;</span>,<span class="string">&#x27;updated&#x27;</span>,<span class="string">&#x27;beforeDestroy&#x27;</span>,<span class="string">&#x27;destroyed&#x27;</span> 这些</span><br><span class="line"><span class="number">2.</span> 除了生命周期之外 我们还有data computed watcher 等等</span><br><span class="line"><span class="number">3.</span> 因为 你调用mixin的时候的时候 混入的方式如下</span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">  created:<span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>)</span>&#123;...&#125;</span><br><span class="line">  data:<span class="function"><span class="keyword">function</span>  (<span class="params"></span>) </span>&#123;...&#125;,</span><br><span class="line">  computed:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;....&#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="number">4.</span> 所以这里我们要根据你 字段不同 才去不同的策略去合并</span><br><span class="line"><span class="number">5.</span> 这里我们先以生命周期为例</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我们先写好策略</span></span><br><span class="line"><span class="comment">// 以下是我们根据 合并不同字段 所采取的策略函数</span></span><br><span class="line"><span class="keyword">const</span> strats = &#123;&#125;</span><br><span class="line"></span><br><span class="line">strats.data = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">strats.computed = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">strats.watch = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 然后是生命周期的</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> LIFECYCLE_HOOKS = [</span><br><span class="line">    <span class="string">&#x27;beforeCreate&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;created&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;beforeMount&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;mounted&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;beforeUpdate&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;updated&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;beforeDestroy&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;destroyed&#x27;</span></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为合并生命周期的策略 基本一样 所以我们循环一下 让他都等于一个函数</span></span><br><span class="line"></span><br><span class="line">LIFECYCLE_HOOKS.forEach( <span class="function"><span class="params">hook</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//这里根据不同的配置合并去才去不同的策略</span></span><br><span class="line">    strats[hook] = mergeHook</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 开始合并</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">mergeOptions</span>(<span class="params">parent,child</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> options = &#123;&#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> parent)&#123; </span><br><span class="line">        mergeField(key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> child)&#123; </span><br><span class="line">        <span class="keyword">if</span>(!parent.hasOwnProperty(key))&#123;</span><br><span class="line">            mergeField(key)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">mergeField</span>(<span class="params">key</span>)</span>&#123;</span><br><span class="line">      <span class="comment">//1.第一次调用Vue.mixin的 会走到便利你调用 Vue.mixin传入的参数里的循环里面去 &#123;created:function a() &#123;&#125;&#125;</span></span><br><span class="line">      <span class="comment">// 2. 这里我们要保证我们的合并策略中有你传入的字段</span></span><br><span class="line">        <span class="keyword">if</span>(strats[key])&#123;</span><br><span class="line">          <span class="comment">// 3.strats[key] 其实就是 strats.created 上面我们已经循环便利好了 所以他是一个函数 也就是我们的  mergeHook  那么 parent[key] 就是你 Vue.options&#123;created:function()&#123;&#125;&#125;函数 第一次的时候是没有 Vue.options.created这个字段的 所以在 mergeHook我们还需要判断 </span></span><br><span class="line">          <span class="comment">// child[key] 就是 你调用 Vue.mixin(&#123;created:function a ()&#123;&#125;&#125;) 传入的 &#123;created:function a ()&#123;&#125;&#125; 中 function a ()&#123;&#125; 函数</span></span><br><span class="line">            options[key] = strats[key](parent[key],child[key])</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">// 没策略 就默认合并</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> options</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="mergeHook函数"><a href="#mergeHook函数" class="headerlink" title="mergeHook函数"></a>mergeHook函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们采用策略的模式来说 你混入对应的选项 就会走到对应的处理函数中去</span><br><span class="line"><span class="number">2.</span> 比如 生命周期的合并就会走到 mergeHook 函数里来</span><br><span class="line"><span class="number">3.</span> 第一次为们调用Vue.mixin(&#123;<span class="attr">created</span>:<span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>) </span>&#123;&#125;&#125;) 的时候 Vue.options 上是没有 字段的 </span><br><span class="line"><span class="number">4.</span> 所以我们调用Vue.mixin之后 就希望 Vue.options&#123;<span class="attr">created</span>:[a函数]&#125;</span><br><span class="line"><span class="number">5.</span> 第二次调用的时候 Vue.options上面已经有东西了 是 Vue.options&#123;<span class="attr">created</span>:[a函数]&#125; 所以当我们在调用 </span><br><span class="line">Vue.mixin(&#123;<span class="attr">created</span>:<span class="function"><span class="keyword">function</span> <span class="title">b</span> (<span class="params"></span>) </span>&#123;&#125;&#125;) 的时候 因为已经 created这个字段了 就希望把第二次调用的时候对应的字段的值 也就是 <span class="function"><span class="keyword">function</span> <span class="title">b</span> (<span class="params"></span>) </span>&#123;&#125; 放到created的后面 Vue.options&#123;<span class="attr">created</span>:[a函数，b函数]&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这里就是开始生命周期的合并</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mergeHook</span> (<span class="params">parentVal,childVal</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//6. 这里就开始我们的合并 第一次的调用 Vue.mixin 的时候 childVal就是我们传入的参数</span></span><br><span class="line">    <span class="keyword">if</span>(childVal)&#123;</span><br><span class="line">        <span class="keyword">if</span>(parentVal)&#123;</span><br><span class="line">            <span class="comment">// 6.1 这里就是 儿子也有 爸爸也有 那么就需要把儿子的值和爸爸的值进行混合一定的爸爸对应字段的方法在前面儿子在后面</span></span><br><span class="line">            <span class="keyword">return</span> parentVal.concat(parentVal)</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">// 6.2 这里就是爸爸没有这个字段 但儿子有 那么直接就向爸爸合并即可</span></span><br><span class="line">            <span class="comment">// 我们距离如果第一次调Vue.mixin 我们的Vue.options 肯定是个空对象 那么我们就需要把儿子的对应字段的函数包装成一个数组</span></span><br><span class="line">            <span class="comment">// 等我们第二次在调Vue.mixin的时候 第二次传入的字段 要向上合并放在第一次调Vue.mixin的包装好的数组的后面</span></span><br><span class="line">            <span class="keyword">return</span> [childVal]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="comment">//如果当你调去mixin的时候 没有传入对应的字段 比如你调用Vue.mixin(&#123;created:function a ()&#123;&#125;&#125;)的时候 created没有处理函数</span></span><br><span class="line">        <span class="comment">// 那么我们直接就返回父亲的即可</span></span><br><span class="line">        <span class="keyword">return</span> parentVal</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="合并实例"><a href="#合并实例" class="headerlink" title="合并实例"></a>合并实例</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们现在把Vue当作对象的时候 调用Vue.mixin 方法 把你每次调用的都合并到Vue.options上</span><br><span class="line"><span class="number">2.</span> 现在我们要合并实例的</span><br><span class="line"><span class="number">3.</span> 也就是当我们在<span class="keyword">new</span> Vue的时候 也会传递一些配置项 这些配置项都是实例上的 比如实例的 data 实例的生命周期 等等等</span><br><span class="line"><span class="number">4.</span> 所以我们现在也要把实例传入的配置项 合并到 Vue.options上</span><br><span class="line"><span class="number">5.</span> 这里我们上面已经写好了合并的方法 </span><br><span class="line"><span class="number">6.</span> 所以这里在初始的时候我们就要进行合并 因为你在 <span class="keyword">new</span> Vue的时候传入了这些</span><br><span class="line"><span class="number">6.</span> 最后合并的结果 其实就是 把实例上传入到了我们的Vue.optionss上 然后在赋值给了我们的实例的$options</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">Vue.mixin(&#123;</span><br><span class="line">    created:<span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;我是第一次调用mixin&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">Vue.mixin(&#123;</span><br><span class="line">    created:<span class="function"><span class="keyword">function</span> <span class="title">b</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;我是第二次调用mixin&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//这里我们在new Vue的时候 也就是创建实例的时候 也会合并到Vue.options上</span></span><br><span class="line"><span class="keyword">let</span> vm = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    el:<span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">    data:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            product:<span class="string">&#x27;3;33&#x27;</span>,</span><br><span class="line">            list:<span class="string">&#x27;333&#x27;</span>,</span><br><span class="line">            name:<span class="string">&#x27;北城&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="title">created</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// init.js </span></span><br><span class="line">Vue.prototype._init = <span class="function"><span class="keyword">function</span> (<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        <span class="comment">//从这里开始我们就要把实例传进来的 合并到我们的 Vue.options上 然后在赋值给我们的实例的$options</span></span><br><span class="line">        <span class="comment">// 这里 vm.constructor.options 就是Vue.options </span></span><br><span class="line">        <span class="comment">// options 就是你创建实例传入的配置项</span></span><br><span class="line">        vm.$options = mergeOptions(vm.constructor.options,options)</span><br><span class="line">  			<span class="comment">// 打印之后 就是 vm.$options.created 现在就有三个个函数了 前两个是我们调用 Vue.mixin的时候created对应的函数 第三个就是 我们实例上的created函数 因为我们实例上还有data 当便利的时候 因为key是data 所以他就走到他对应的策略里面去了</span></span><br><span class="line">        <span class="built_in">console</span>.log(vm.$options,<span class="string">&#x27;---&#x27;</span>)</span><br><span class="line">        initState(vm)</span><br><span class="line">        <span class="keyword">if</span>(vm.$options.el)&#123;</span><br><span class="line">            vm.$mount(vm.$options.el)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="全局组件和局部组件"><a href="#全局组件和局部组件" class="headerlink" title="全局组件和局部组件"></a>全局组件和局部组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里我们已经知道了上面的合并 会循环配置项里的key 然后一次向Vue.options上合并 然后在 赋值给我们的实例的 $options上</span><br><span class="line"><span class="number">2.</span> 我们在注册全局组件的时候使用的Vue.components这样注册组件在全局都可以用的</span><br><span class="line"><span class="number">3.</span> 学过上面的合并我们知道 其实就是在初始化的时候 我们吧 Vue.components 全局组件 合并到了 实例上component</span><br></pre></td></tr></table></figure>
<h2 id="生命周期的调用"><a href="#生命周期的调用" class="headerlink" title="生命周期的调用"></a>生命周期的调用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 以上我们就把生命周期都合并了到了实例的$options 每个字段的对应的值合并之后都是数组</span><br><span class="line"><span class="number">2.</span> 现在我们针对不同的生命周期去在不同的初始化的时候调用</span><br><span class="line"><span class="number">3.</span> 所以这里我们需要有一个调用的方法 callHook</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// lifecycle.js</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//2021 0220 调用生命周期的方法</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">callHook</span>(<span class="params">vm,hook</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 1. 如果你传入了生命周期比如 created </span></span><br><span class="line">    <span class="comment">// 那么我们在new Vue的时候 已经合并到当前实例的$options上了 </span></span><br><span class="line">    <span class="comment">// vm.$options.created:[a函数,b函数]</span></span><br><span class="line">    <span class="keyword">const</span> handlers = vm.$options[hook]</span><br><span class="line">    <span class="comment">//2. 然后开始循环 调用</span></span><br><span class="line">    <span class="keyword">if</span>(handlers)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i&lt;handlers.length;i++)&#123;</span><br><span class="line">            <span class="comment">// handlers[i] 就是我们合并的每一个字段 因为合并完之后都变成数组了</span></span><br><span class="line">            <span class="comment">// 在调用的时候 我们也要保证函数里面的this 是我们的实例</span></span><br><span class="line">            handlers[i].call(vm)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// init.js</span></span><br><span class="line"></span><br><span class="line">Vue.prototype._init = <span class="function"><span class="keyword">function</span> (<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        vm.$options = mergeOptions(vm.constructor.options,options)</span><br><span class="line">        <span class="comment">//2021 0220 调用生命周期 经过上面的合并操作 我们vm.$options 就有你传入的配置项的字段 然后我们就可以调用了</span></span><br><span class="line">        callHook(vm,<span class="string">&#x27;beforeCreated&#x27;</span>)</span><br><span class="line">        <span class="comment">//初始化我们的状态 data methods computed watch等等</span></span><br><span class="line">        initState(vm)</span><br><span class="line">        <span class="comment">// 2021 0220 上面状态初始完毕了 我们就可以调用 created了</span></span><br><span class="line">        callHook(vm,<span class="string">&#x27;created&#x27;</span>)</span><br><span class="line">        <span class="comment">//8. 我们初始化了 State以后就要开始 首次渲染</span></span><br><span class="line">        <span class="comment">//8.1 如果当前有el属性 我才渲染</span></span><br><span class="line">        <span class="keyword">if</span>(vm.$options.el)&#123;</span><br><span class="line">            vm.$mount(vm.$options.el)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// lifecycle.js </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">mountComponent</span>(<span class="params">vm,el</span>)</span>&#123;</span><br><span class="line">   <span class="keyword">let</span> updateComponent = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">       <span class="comment">//2021 0220 生成真实DOM之前我们可以调用 beforeMounted</span></span><br><span class="line">        callHook(vm,<span class="string">&#x27;beforeMounted&#x27;</span>)</span><br><span class="line">        vm._update(vm._render())</span><br><span class="line">        <span class="comment">//2021 0220 上面update函数调用完毕之后我们就调用mounted</span></span><br><span class="line">        callHook(vm,<span class="string">&#x27;mounted&#x27;</span>)</span><br><span class="line">   &#125;</span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">   <span class="keyword">new</span> Watcher(vm,updateComponent,<span class="function">() =&gt;</span> &#123;&#125;,<span class="literal">true</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们生命周期的原理 简单来说 就是一个发布订阅的模式</span><br><span class="line"><span class="number">2.</span> 首先把我们的Vue 当作对象去使用 然后在上面也有一个options属性</span><br><span class="line"><span class="number">3.</span> 然后通过调用mixin函数 合并我们的配置项 </span><br><span class="line"><span class="number">4.</span> 核心api 就是 mergeOptions 函数 他会 把你调用 mixin 传进来的 配置项 然后合并到 Vue.options上 然后返回这个 options上属性 </span><br><span class="line"><span class="number">5.</span> 相同配置项项的属性 最后会合并成一个数组</span><br><span class="line"><span class="number">6.</span> 合并实例的时候的 也是在initState之前 就调用mergeOptions函数 把 你<span class="keyword">new</span> Vue的时候传进来的选项 合并到 Vue.options 最后在赋值给我们实例的 $options上 这样我们实例的$options就有合并和的</span><br><span class="line"><span class="number">6.</span> 然后我们在写一个调用的方法 然后别在 <span class="keyword">new</span> Vue不同的时期去调用这些 生命周期函数</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Vue源码开发-生命周期合并07</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/">http://example.com/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2021-01-06</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2021-01-06</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue/">Vue</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue源码开发-数组的依赖收集08</div></div></a></div><div class="next-post pull-right"><a href="/2021/01/05/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%89%B9%E9%87%8F%E6%9B%B4%E6%96%B006/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue源码开发-批量更新06</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img class="cover" src="/img/gin-vue-admin.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-16</div><div class="title">Gin-Vue-Admin</div></div></a></div><div><a href="/2021/01/08/Vue源码开发之组件的渲染10/" title="Vue源码开发-组件的渲染10"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-08</div><div class="title">Vue源码开发-组件的渲染10</div></div></a></div><div><a href="/2021/01/07/Vue源码开发之组件的合并策略09/" title="Vue源码开发-组件合并策略09"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-07</div><div class="title">Vue源码开发-组件合并策略09</div></div></a></div><div><a href="/2021/01/06/Vue源码开发之数组的依赖收集08/" title="Vue源码开发-数组的依赖收集08"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-06</div><div class="title">Vue源码开发-数组的依赖收集08</div></div></a></div><div><a href="/2021/01/05/Vue源码开发之批量更新06/" title="Vue源码开发-批量更新06"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Vue源码开发-批量更新06</div></div></a></div><div><a href="/2021/01/04/Vue源码开发之对象依赖收集05/" title="Vue源码开发-数据收集依赖05"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-04</div><div class="title">Vue源码开发-数据收集依赖05</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B6"><span class="toc-text">Vue源码开发之生命周期合并</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%B7%E5%90%88%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-text">混合的概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#initGlobalApi%E5%87%BD%E6%95%B0"><span class="toc-text">initGlobalApi函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#mergeOptions"><span class="toc-text">mergeOptions</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F"><span class="toc-text">策略模式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#mergeHook%E5%87%BD%E6%95%B0"><span class="toc-text">mergeHook函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%88%E5%B9%B6%E5%AE%9E%E4%BE%8B"><span class="toc-text">合并实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E7%BB%84%E4%BB%B6%E5%92%8C%E5%B1%80%E9%83%A8%E7%BB%84%E4%BB%B6"><span class="toc-text">全局组件和局部组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">生命周期的调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>